﻿using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;

namespace DataAccess.DAL
{
    public class PermissioninRoleDAL : BaseDAL
    {
        //public PermissioninRole Get(int id)
        //{
        //    return CONN_TopicSharing.Get<PermissioninRole>(id);
        //}

        //public bool Insert(PermissioninRole permissioninRole)
        //{
        //    return CONN_TopicSharing.Insert<PermissioninRole>(permissioninRole);
        //}

        //public bool Update(PermissioninRole permissioninRole)
        //{
        //    return CONN_TopicSharing.Update<PermissioninRole>(permissioninRole);
        //}

        public void SetPermissioninRole(int roleid,List<PermissioninRole> permissioninRoles)
        {
            CONN_TopicSharing.Delete<PermissioninRole>(p => p.role_id == roleid);
            foreach (var item in permissioninRoles)
            {
                CONN_TopicSharing.Insert<PermissioninRole>(item);
            }
        }


        

        public List<PermissioninRole> GetPermissioninRoles(int role_id)
        {
            StringBuilder sb = new StringBuilder();
            
            sb.Append(" select ");
            sb.Append(" pr.* ");
            sb.Append(" ,p.id p_id,	p.parent_id p_parent_id,p.name p_name,p.menu_name p_menu_name,p.type p_type,p.action p_action,p.controller p_controller,p.target p_target,p.url p_url,p.sort p_sort,p.is_hidden p_is_hidden,p.remark p_remark,p.icon p_icon ");
            sb.Append(" ,r.id r_id,r.name r_name,r.state r_state,r.add_data r_add_data,r.by_id r_by_id,r.is_delete r_is_delete  ");
            sb.Append(" from PermissioninRole pr  ");
            sb.Append(" left join Permission p on p.id=pr.permission_id ");
            sb.Append(" left join Role r on r.id=pr.role_id ");
            sb.Append(" where r.is_delete=0 and r.id=@role_id ");

            DynamicParameters dp = new DynamicParameters();
            dp.Add("@role_id", role_id);

            return CONN_TopicSharing.SQLQuery<PermissioninRole>(sb.ToString(), dp).ToList();
        }

        public List<Permission> GetPermissions(int role_id)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" select p.* ");
            sb.Append(" from Permission p  ");
            sb.Append(" left join PermissioninRole pr on p.id=pr.permission_id ");
            sb.Append(" left join Role r on r.id=pr.role_id  ");
            sb.Append(" where r.is_delete=0 and r.id=@role_id ");
             
            DynamicParameters dp = new DynamicParameters();
            dp.Add("@role_id", role_id);

            return CONN_TopicSharing.SQLQuery<Permission>(sb.ToString(), dp).ToList();
        }


        public List<PermissioninRole> GetCurPermissioninRoles(int role_id)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" select ");
            sb.Append("  * ");
            sb.Append(" from PermissioninRole ");

            sb.Append(" where role_id=@role_id ");

            DynamicParameters dp = new DynamicParameters();
            dp.Add("@role_id", role_id);

            return CONN_TopicSharing.SQLQuery<PermissioninRole>(sb.ToString(), dp).ToList();
        }


        public bool SetPermissioninRole(int roleid, List<int> permissioninIds)
        {
            CONN_TopicSharing.Delete<PermissioninRole>(p => p.role_id == roleid);
            foreach (var item in permissioninIds)
            {
                CONN_TopicSharing.Insert<PermissioninRole>(new PermissioninRole { role_id = roleid, permission_id = item });
            }

            return true;
        }
    }
}
